﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Sublimation
{
    public class EnemyUnitSmallWhipGal : EnemyUnit
    {
        new protected int NUMBER_OF_FRAMES_TO_PLAY = 4;
        new protected const int FRAMES_PER_SECOND = 10;
        protected float range = 200;
        protected Tower target;
        protected float rangeSpeed = 500;
        protected float rangedAttackTimerMillis = 0;

        public EnemyUnitSmallWhipGal(WorldProperties worldProperties, EnemyKind kind)
            : base(worldProperties, kind)
        {
        }

        public EnemyUnitSmallWhipGal(WorldProperties worldProperties,EnemyKind kind, int spawnPoint)
            : base(worldProperties, kind, spawnPoint){
        }

        protected override void LoadContent()
        {
            base.LoadContent();
            Origin = new Vector2(60, 29);
            textureAnimator = new AnimatedSprite.AnimatedTexture(Origin, Rotation, Scale, 1.0f);
            textureAnimator.Load(Game.Content, "Sprites\\enemy2", NUMBER_OF_FRAMES_TO_PLAY, FRAMES_PER_SECOND);
            textureAttack = new AnimatedSprite.AnimatedTexture(new Vector2(44, 40), Rotation, Scale, 1.0f);
            textureAttack.Load(Game.Content, "Sprites\\enemy2_attack", 6, FRAMES_PER_SECOND);
            Scale = 0.5f;
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            rangedAttackTimerMillis += gameTime.ElapsedGameTime.Milliseconds;

            getTarget();

            if (rangedAttackTimerMillis > meleeAttackDelay)
            {
                rangedAttackTimerMillis = 0;
                if (target != null)
                {
                    RangedAttack();
                }
                else
                {
                   attacking = false;
                }
            }
        }
        public override void DoMovement(Microsoft.Xna.Framework.GameTime gameTime)
        {
            if (target != null)
            {
                attacking = true;
            }
            else
            {
                base.DoMovement(gameTime);
            }
        }
         public void getTarget()
        {
            target = null;
            float minDistance=0;
            float distance;
            foreach (Tower tower in worldProperties.Players[0].Towers)
            {
                distance = Vector2.Distance(tower.Position, position);
                if (distance < range)
                {
                    if (target == null ||distance < minDistance)
                    {
                        target = tower;
                        minDistance = distance;
                    }
                }
            }

        }


         public void RangedAttack()
         {
             Vector2 bulletVelocity = target.Position - Position;
             bulletVelocity.Normalize();
             if (bulletVelocity.Y > 0)
             {
                 Rotation = (float)Math.Acos(bulletVelocity.X);
             }
             else
             {
                 Rotation = -(float)Math.Acos(bulletVelocity.X);
             }
             new WhipBall(worldProperties, false, position, bulletVelocity, rangeSpeed, new Vector2(10, 10), range, meleeDamage);

        }

        public override void Kill()
        {
            base.Kill();
            if (Life <= 0) new Animation(worldProperties, this, 0.8f, 4, 10, "Sprites\\enemy2_dying");
        }
    }
}
